package syntaxcolor.v4.syntaxcolor.token;

/**
 * 字符串分段，包括偏移量（从开始计算），长度及分段类型（样式）
 * 
 * @author wyl
 */
public class Token {

  public Token(int offset, int length, String name) {
    super();
    this.offset = offset;
    this.length = length;
    this.name = name;
  }

  private int    offset;

  private int    length;

  private String name;

  /**
   * offset get
   * 
   * @return offset
   */
  public int getOffset() {
    return offset;
  }

  /**
   * length get
   * 
   * @return length
   */
  public int getLength() {
    return length;
  }

  /**
   * name get
   * 
   * @return name
   */
  public String getName() {
    return name;
  }

  public void increaseLength() {
    this.length++;
  }

  /**
   * 平移分段，也就是调整分段的offset
   * 
   * @param shiftSize 平移量，+增加offset，-减少offset
   * @return
   */
  public boolean shift(int shiftSize) {
    if (this.offset + shiftSize < 0) {
      return false;
    }
    this.offset += shiftSize;
    return true;
  }

  /**
   * 判断指定分段在平移后是否与当前分段一致
   * 
   * @param token
   * @param shift
   * @return
   */
  public boolean equalsWithShift(Token token, int shift) {
    return this.length == token.length && this.name.equals(token.name)
        && this.offset == token.offset + shift;
  }

}
